<?php
/**
 * Created by PhpStorm.
 * User: beyondwin
 * Date: 19/6/13
 * Time: 上午11:09
 * XiangMu190616
 *
 *  wx9cf58f08b6f9eadb
 *  3c31b21396bff1bac0b6fdd96df8ce0c
 */
//定义常量token

define('TOKEN','weixin');

//检查标签
function checkSignature()
{
    //先获取到这三个参数
    $signature = $_GET['signature'];
    $nonce = $_GET['nonce'];
    $timestamp = $_GET['timestamp'];

    //把这三个参数存到一个数组里面
    $tmpArr = array($timestamp,$nonce,TOKEN);
    //进行字典排序
    sort($tmpArr);

    //把数组中的元素合并成字符串，impode()函数是用来将一个数组合并成字符串的
    $tmpStr = implode($tmpArr);

    //sha1加密，调用sha1函数
    $tmpStr = sha1($tmpStr);
    //判断加密后的字符串是否和signature相等
    if($tmpStr == $signature)
    {

        return true;
    }
    return false;
}


/**
 * 响应用户发来的消息
 */
function responseMsg()
{
    //获取post过来的数据，它一个XML格式的数据
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    //将数据打印到log.xml
    Utils::logger($postStr);
    if (!empty($postStr)){
        //将XML数据解析为一个对象
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $RX_TYPE = trim($postObj->MsgType);
        //消息类型分离
        switch($RX_TYPE){
            case "event": //事件
                $result = $this->receiveEvent($postObj);
                break;
            case "text": //文本
                $result = $this->receiveText($postObj);
                break;
            case "image": //图片
                $result = $this->receiveImage($postObj);
                break;
            case "voice": //语音
                $result = $this->receiveVoice($postObj);
                break;
            case "video": //视频
                $result = $this->receiveVideo($postObj);
                break;
            case "location": //地理位置
                $result = $this->receiveLocation($postObj);
                break;
            case "link": //链接
                $result = $this->receiveLink($postObj);
                break;
            default:
                $result = "unknow msg type:".$RX_TYPE;
                break;
        }
        //打印输出的数据到log.xml
        Utils::logger($result, '公众号');
        echo $result;
    }else{
        echo "";
        exit;
    }
}

/**
 * 接收文本消息,通过发送指定文本，
 * 让服务器回复相应类型的信息
 */
function receiveText($object)
{
    $keyword = trim($object->Content);
    if (strstr($keyword, "文本")){
        $content = "这是个文本消息";
        //回复文本
        $result = $this->transmitText($object, $content);
    }else if (strstr($keyword, "单图文")){
        $content = array();
        $content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容",
            "PicUrl"=>"http://weiweiyi.duapp.com/images/img1.jpg", "Url"=>"http://weiweiyi.duapp.com/test.php");
        //回复单图文
        $result = $this->transmitNews($object, $content);
    }else if (strstr($keyword, "多图文")){
        $content = array();
        $content[] = array("Title"=>"多图文1标题", "Description"=>"多图文1描述",
            "PicUrl"=>"http://weiweiyi.duapp.com/images/img1.jpg", "Url"=>"http://weiweiyi.duapp.com/test.php");
        $content[] = array("Title"=>"多图文2标题", "Description"=>"多图文2描述",
            "PicUrl"=>"http://weiweiyi.duapp.com/images/img2.jpg", "Url"=>"http://weiweiyi.duapp.com/test.php");
        $content[] = array("Title"=>"多图文3标题", "Description"=>"多图文3描述",
            "PicUrl"=>"http://weiweiyi.duapp.com/images/img3.jpg", "Url"=>"http://weiweiyi.duapp.com/test.php");
        //回复多图文
        $result = $this->transmitNews($object, $content);
    }else if (strstr($keyword, "音乐")){
        $content = array("Title"=>"好想你", "Description"=>"歌手：朱主爱",
            "MusicUrl"=>"http://weiweiyi.duapp.com/music/missyou.mp3",
            "HQMusicUrl"=>"http://weiweiyi.duapp.com/music/missyou.mp3");
        //回复音乐
        $result = $this->transmitMusic($object, $content);
    }else {
        $content = date("Y-m-d", time())."\n".$object->FromUserName."\n-lzc";
        //回复文本
        $result = $this->transmitText($object, $content);
    }
    return $result;
}

if (isset($_GET["echostr"])){
    //如果相等，验证成功就返回echostr
    if(checkSignature())
    {
        //返回echostr
        $echostr = $_GET['echostr'];
        if($echostr)
        {
            echo $echostr;
            exit;
        }
    }
}else {
    responseMsg();
}


